﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebShopAPI.Models;//
using DAL; // 数据服务层
using Models;
namespace WebShopAPI.Controllers
{
    /// <summary>
    /// 权限 所需要 提供API类
    /// </summary>
    public class rightsController : BaseWebApiController
    {
        sp_permissionDAL permissionDAL = new sp_permissionDAL();

        #region  1 根据类型返回不同结构的菜单的方法 提供数据方法
        public ResponData Get(string type = "list")
        {
            //获取所有权限
            List<sp_permission> allPer = permissionDAL.GetAll();
            ResponData rp = new ResponData();
            if (type == "list")
            {
                rp.data = allPer.Select(p => new
                {
                    id = p.ps_id,
                    authName = p.ps_name,
                    level = p.ps_level,
                    pid = p.ps_pid,
                    path =p.ps_c

                });
                rp.meta = new Meta
                {
                    msg = "列表读取菜单成功",
                    status = 200
                };
            }
            else //以树结构返回
            {
                #region 以树结构返回
                //读取一级所有数据
                List<sp_permission> firstPer = allPer.Where(p => p.ps_pid == 0).ToList(); //一级的父级为0
                                                                                          //创建要返回的数据集合，并进行处理
                List<MyMenuItem> allmenu = new List<MyMenuItem>();
                //通过一级循环
                foreach (sp_permission obj in firstPer)
                {     
                    MyMenuItem menu = new MyMenuItem()
                    {
                        id = obj.ps_id,
                        authName = obj.ps_name,
                        path = null,
                        pid = "0"
                        // path = "/" + obj.ps_c + "/" + obj.ps_a //二级才有
                    };
                    //读取该一级数据的二级
                    var secondePer = allPer.Where(p => p.ps_pid == obj.ps_id).Select(
                        p => new MyMenuItem
                        {
                            id = p.ps_id,
                            authName = p.ps_name,
                            path = null, //
                            pid = obj.ps_id+"",
                            children=allPer.Where(p3=>p3.ps_pid==p.ps_id).Select(p4=>new MyMenuItem 
                            {
                                id = p4.ps_id,
                                authName = p4.ps_name,
                                path = null, //二级才有
                                pid = obj.ps_id+","+p.ps_id,

                            }).ToList()
                        }
                        );
                    //修改一级的二级菜单集合
                    menu.children = secondePer.ToList();
                    ///////////////////////////////////////
                    allmenu.Add(menu);//加入到所有菜单集合中  

                }
                rp.data = allmenu;
                rp.meta = new Meta
                {
                    msg = "树结构读取菜单成功",
                    status = 200
                };
                #endregion
            }

            //
            return rp;
        }
        #endregion
    }
}
